<?php

/**
 * Template Controller.
 *
 * @version  $Id: template.php 2 2009-10-02 23:06:43Z perfilev $
 * @package  System
 */

abstract class Controller_Template extends Controller
{
	/**
	 * @var  string  Page template.
	 */
	protected $template = 'template';
	
	/**
	 * @var  bool  Auto render template.
	 **/
	protected $autoRender = true;

	/**
	 * Loads the template View object.
	 *
	 * @return  void
	 */
	public function before()
	{
		$this->template = new View($this->template);
	}

	/**
	 * Assigns the template as the request response.
	 *
	 * @return  void
	 */
	public function after()
	{
		if ($this->autoRender) {
			$this->response->body = $this->template->render();
		}
	}

	public function addScript($script) {
		$scripts = $this->template->scripts;
		if ($scripts === null) {
			$scripts = array();
		}
		if (!in_array($script, $scripts)) {
			$scripts[] = $script;
			$this->template->scripts = $scripts;
		}
	}
	
	public function addLink($link) {
		$links = $this->template->link;
		if ($links === null) {
			$links = array();
		}
		if (!isset($links[$link['href']])) {
			$links[$link['href']] = $link;
		 	$this->template->link = $links;
		}
	}
		
	public function addStyle($href) {
		$this->addLink(array(
			'href' => $href,
			'rel'  => 'stylesheet',
			'type' => 'text/css',
		));
	}
}
